<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="css/pagination.css">
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script type="text/javascript">
    $(function () {
        // 1 为分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 10;
        window.keyword = "";

        // 2 调用执行分页的函数，显示分页效果
        generatePage();

        // 3 给查询按钮绑定单击响应函数
        $("#searchBtn").click(function () {
            window.keyword = $("#keywordInput").val();
            generatePage();
        });

        // 4 点击新增按钮打开模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show");
        });

        // 5 给新增模态框中的保存按钮绑定单击响应函数
        $("#saveRoleBtn").click(function () {
            var roleName = $.trim($("#addModal [name=roleName]").val());
            if (roleName == "" || roleName == null) {
                layer.msg("请输入角色名称！");
                return;
            }
            $.ajax({
                "url": "role/save.json",
                "type": "post",
                "data": {
                    "name": roleName
                },
                "dataType": "json",
                "async": false,
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("新增成功！");
                        window.pageNum = 99999999;
                        generatePage();
                    }
                    if (result == "FAILED") {
                        layer.msg("新增失败！ " + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            $("#addModal").modal("hide");
            $("#addModal [name=roleName]").val("");
        });

        // 6 给页面上的“铅笔”按钮绑定单击响应函数
        $("#rolePageBody").on("click", ".pencilBtn", function () {
            $("#editModal").modal("show");
            var roleName = $(this).parent().prev().text();
            $("#editModal [name=roleName]").val(roleName);
            window.roleId = this.id;
        });

        // 7 给更新模态框中的更新按钮绑定单击响应函数
        $("#updateRoleBtn").click(function () {
            var roleName = $("#editModal [name=roleName]").val();
            $.ajax({
                "url": "role/update.json",
                "type": "post",
                "data": {
                    "id": window.roleId,
                    "name": roleName
                },
                "dataType": "json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("更新成功！");
                        generatePage();
                    }
                    if (result == "FAILED") {
                        layer.msg("更新失败！ " + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            $("#editModal").modal("hide");
        });

        // 8 点击确认模态框中的确认按钮执行删除
        $("#removeRoleBtn").click(function () {
            var requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url": "role/remove/role/by/id/array.json",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("删除成功！");
                        generatePage();
                    }
                    if (result == "FAILED") {
                        layer.msg("删除失败！ " + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText);
                }
            });
            $("#confirmModal").modal("hide");
        });

        // 9 给单条删除按钮绑定单击响应函数
        $("#rolePageBody").on("click",  ".removeBtn", function () {
            var roleName = $(this).parent().prev().text();
            var roleArray = [{
                "roleId": this.id,
                "roleName":roleName
            }];
            showConfirmModal(roleArray);
        });

        // 10 给总的checkbox绑定单击响应函数
        $("#summaryBox").click(function () {
            var currentStatus = this.checked;
            $(".itemBox").prop("checked", currentStatus);
        });

        // 11 全选、全不选的反向操作
        $("#rolePageBody").on("click", ".itemBox", function () {
            var checkedBoxCount = $(".itemBox:checked").length;
            var totalBoxCount = $(".itemBox").length;
            $("#summaryBox").prop("checked", checkedBoxCount == totalBoxCount);
        });

        // 12 给批量删除的按钮绑定单击响应函数
        $("#batchRemoveBth").click(function () {
            var roleArray = [];
            $(".itemBox:checked").each(function (){
                var roleId = this.id;
                var roleName = $(this).parent().next().text();
                roleArray.push({
                    "roleId": roleId,
                    "roleName": roleName
                });
            });
            if (roleArray.length == 0) {
                layer.msg("请选择要删除的数据！");
                return ;
            }
            showConfirmModal(roleArray);
        });

        // 13 给分配权限按钮绑定单击响应函数
        $("#rolePageBody").on("click", ".checkBtn", function () {
            window.roleId = this.id;
            $("#assignModal").modal("show");
            fillAuthTree();
        });

        // 14 给分配权限模态框中的保存按钮绑定单击响应函数
        $("#assignBtn").click(function () {
            var authIdArray = [];
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            var checkedNodes = zTreeObj.getCheckedNodes();
            for (var i = 0; i < checkedNodes.length; i++) {
                var authId = checkedNodes[i].id;
                authIdArray.push(authId);
            }
            var requestBody = {
                "authIdArray": authIdArray,
                "roleId": [window.roleId]
            };
            requestBody = JSON.stringify(requestBody);
            $.ajax({
                "url": "assign/do/role/auth.json",
                "type": "post",
                "data": requestBody,
                "contentType": "application/json;charset=UTF-8",
                "dataType": "json",
                "success": function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("分配成功！")
                    }
                    if (result == "FAILED") {
                        layer.msg("分配失败！" + response.message);
                    }
                },
                "error": function (response) {
                    layer.msg(response.status + " " + response.statusText)
                }
            });
            $("#assignModal").modal("hide");
        });
    });
</script>

<body>

<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button id="batchRemoveBth" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button id="showAddModalBtn" type="button" class="btn btn-primary" style="float:right;"><i
                            class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox" checked="false"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination" style="margin: 0px auto;">
                                        <!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/model-role-add.jsp" %>
<%@include file="/WEB-INF/model-role-edit.jsp" %>
<%@include file="/WEB-INF/model-role-confirm.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</body>
</html>
